package com.syaccp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syaccp.mapper.CustomerImMapper;
import com.syaccp.mapper.CustomerMapper;
import com.syaccp.model.Customer;
import com.syaccp.model.CustomerIm;
import com.syaccp.model.SysUser;
import com.syaccp.request.CustomerDetailSelectRequest;
import com.syaccp.request.CustomerImAddRequest;
import com.syaccp.request.CustomerImDeleteRequest;
import com.syaccp.request.CustomerImUpdateRequest;
import com.syaccp.response.CustomerImResponse;
import com.syaccp.service.CustomerImService;
import com.syaccp.service.CustomerService;
import com.syaccp.utils.Result;
import com.syaccp.utils.SecurityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CustomerImServiceImpl  extends ServiceImpl<CustomerImMapper, CustomerIm> implements CustomerImService {

    @Resource
    private CustomerService cs;



    @Override
    public Result findImByCustomerId(CustomerDetailSelectRequest customerDetailSelectRequest) {
        PageHelper.startPage(customerDetailSelectRequest.getPage(), customerDetailSelectRequest.getLimit());

        QueryWrapper qw=new QueryWrapper();
        qw.eq("customer_id", customerDetailSelectRequest.getId());
        //查询未删除的数据
        qw.eq("del_flag",0);
        PageInfo pageInfo = new PageInfo<>(this.baseMapper.selectList(qw));
        List<CustomerImResponse> list=new ArrayList<>();
        for (Object im: pageInfo.getList()) {
            CustomerImResponse cir=new CustomerImResponse();
            BeanUtils.copyProperties(im,cir);
            list.add(cir);
        }
        pageInfo.setList(list);

        return Result.success(pageInfo);
    }

    @Override
    public Result updateIm(CustomerImUpdateRequest customerImUpdateRequest) {
        SysUser sysUser= SecurityUtil.getLoginUser();
        CustomerIm ci=new CustomerIm();
        BeanUtils.copyProperties(customerImUpdateRequest,ci);

        ci.setUpdateBy(sysUser.getId());
        ci.setUpdateTime(new Date());
        int i = this.baseMapper.updateById(ci);
        if (i==0){
            return Result.error();
        }
        return Result.success(i);
    }

    @Override
    @Transactional
    public Result addIm(CustomerImAddRequest customerImAddRequest) {
        CustomerIm ci=new CustomerIm();
        BeanUtils.copyProperties(customerImAddRequest,ci);
        //为客户列表子账号数量加一
        Customer customer = cs.getBaseMapper().selectById(customerImAddRequest.getCustomerId());
        customer.setAccountCount(customer.getAccountCount()+1);
        int i1 = cs.getBaseMapper().updateById(customer);
        if (i1==0){
            throw new RuntimeException();
        }
        //生成客户账号
        Calendar calender = Calendar.getInstance();
        String no="";
        String month=calender.get(Calendar.MONTH)+"";
        if (month.length()==1){
            month="0"+month;
        }
        no+=month;
        String date=calender.get(Calendar.DATE)+"";
        if (date.length()==1){
            date="0"+date;
        }
        no+=date;
        Random r = new Random();
        for (int i=0;i<4;i++){
            // 通过对象方法获取随机数
            int data = r.nextInt(10);
            no+=data;
        }
        ci.setAccount(no);
        //新增客户账号
        int insert = this.baseMapper.insert(ci);
        if (insert==0){
            throw new RuntimeException();
        }
        return Result.success(insert);
    }

    @Override
    @Transactional
    public Result deleteIm(CustomerImDeleteRequest customerImDeleteRequest) {

        //为客户列表子账号数量减一
        Customer customer = cs.getBaseMapper().selectById(customerImDeleteRequest.getCustomerId());
        customer.setAccountCount(customer.getAccountCount()-1);
        int i1 = cs.getBaseMapper().updateById(customer);
        if (i1==0){
            throw new RuntimeException();
        }
        //删除客户账号
        int insert = this.baseMapper.deleteById(customerImDeleteRequest.getId());
        if (insert==0){
            throw new RuntimeException();
        }
        return Result.success(insert);
    }


}
